﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace omaura_oled
{
    /// <summary>
    ///  Class to read and access the language text files
    /// Read the Informations from the .\Lang\"lang".txt file
    /// </summary>
    public class Lang
    {
        #region "Attributes"
        //-- parent
        public OmauraOled OledInterface;
        public string PathToLangFile = "";

        //-- pages
        //---status
        public string StatusMainTitle = "";
        public string StatusOledPanelTitle = "";
        public string StatusOledPaneltextConnected = "";
        public string StatusOledPanelTextDisconnected = "";
        public string StatusUsbParametersTitle = "";
        public string StatusUsbParametersTitlePort = "";
        public string StatusUsbParametersTitleSpeed = "";
        public string StatusUsbParametersTitleStatus = "";
        public string StatusWeatherButton = "";
        //--- update
        public string UpdateMainTitle = "";
        //--- about
        public string AboutMainTitle = "";
        public string AboutVersionTitle = "";
        public string AboutOmauraButton = "";
        public string AboutSupportButton = "";
        //--- tools
        public string ToolsMainTitle = "";
        public string ToolsGeneralTitle = "";
        public string ToolsGeneralOledTitle = "";
        public string ToolsGeneralOledEnable = "";
        public string ToolsGeneralOledDisable = "";
        public string ToolsGeneralBackgroundTitle = "";
        public string ToolsGeneralBackgroundEnable = "";
        public string ToolsGeneralBackgroundDisable = "";
        public string ToolsGeneralLanguageTitle = "";
        public string ToolsMessagesTitle = "";
        public string ToolsMessageStartTitle = "";
        public string ToolsMessageStartMessages = "";
        public string ToolsMessageStartLogo = "";
        public string ToolsMessageTextsizeTitle = "";
        public string ToolsMessageTextsizeBig = "";
        public string ToolsMessageTextsizeMedium = "";
        public string ToolsMessageTextsizeSmall = "";
        public string ToolsThemesTitle = "";
        public string ToolsWeatherTitle = "";
        public string ToolsWeatherCity = "";
        public string ToolsWeatherTemperature = "";
        public string ToolsWeatherTemperatureCelcius = "";
        public string ToolsWeatherTemperatureFahrenheit = "";
        public string ToolsGeneralButton = "";
        public string ToolsMessagesButton = "";
        public string ToolsThemesButton = "";
        public string ToolsWeatherButton = "";

        //-- navigation
        //--- normal
        public string NavigationNormalStatus = "";
        public string NavigationNormalUpdate = "";
        public string NavigationNormalAbout = "";
        //--- tools
        public string NavigationToolsRestore = "";
        //--
        #endregion "Attributes"

        #region "Constructor"
        //--
        public Lang(string Language, OmauraOled ParentOled)
        {
            OledInterface = ParentOled;
            LoadLangInterface(Language);
        }
        //--
        #endregion "Constructor"

        #region "Methods"
        //--
        public void LoadLangInterface(string Language)
        {
            PathToLangFile = ".\\Resources\\Lang\\" + Language + ".txt";

            #region "pages"
            //--

            #region "status"
            //-- labels
            OledInterface.labelSTitle.Content = ReadLangWithParameter("StMaTi");
            OledInterface.labelSDevice.Content = ReadLangWithParameter("StOpTi");
            OledInterface.labelSUSB.Content = ReadLangWithParameter("StUpTi");
            OledInterface.labelSUSBPort.Content = ReadLangWithParameter("StUptP");
            OledInterface.labelSUSBTransfert.Content = ReadLangWithParameter("StUptS");
            OledInterface.labelSUSBStatus.Content = ReadLangWithParameter("StUpts");
            //-- parameters
            //
            // HERE SAVE THE PARAMETERS
            //
            //-- buttons
            OledInterface.ButtonWeather.Content = ReadLangWithParameter("StWeBu");
            //--
            #endregion "status"

            #region "update"
            //-- labels
            OledInterface.labelUTitle.Content = ReadLangWithParameter("UpMaTi");
            OledInterface.labelUVersion.Content = ReadLangWithParameter("AbVeTi") + " " + OledInterface.Version;
            OledInterface.buttonUSearch.Content = ReadLangWithParameter("UpBuSe");
            //--
            #endregion "update"

            #region "about"
            //-- labels
            OledInterface.labelATitle.Content = ReadLangWithParameter("AbMaTi");
            OledInterface.labelAVersion.Content = ReadLangWithParameter("AbVeTi") + " " + OledInterface.Version;
            //-- buttons
            OledInterface.buttonAOmaura.Content = ReadLangWithParameter("AbOmBu");
            OledInterface.buttonAHelp.Content = ReadLangWithParameter("AbSuBu");
            OledInterface.buttonAHelpHTML.Content = ReadLangWithParameter("AbHeBu");
            //--
            #endregion "about"

            #region "tools"
            //-- labels and radiobuttons
            OledInterface.labelTTitle.Content = ReadLangWithParameter("ToMaTi");
            OledInterface.labelTGeneralTitle.Content = ReadLangWithParameter("ToGeTi");
            OledInterface.labelTGeneralOnOff.Content = ReadLangWithParameter("ToGeOT");
            OledInterface.radioOledActiveOn.Content = ReadLangWithParameter("ToGeOE");
            OledInterface.radioOledActiveOff.Content = ReadLangWithParameter("ToGeOD");
            OledInterface.labelTBackgroundOnOff.Content = ReadLangWithParameter("ToGeBT");
            OledInterface.radioBgOn.Content = ReadLangWithParameter("ToGeBE");
            OledInterface.radioBgOff.Content = ReadLangWithParameter("ToGeBD");
            OledInterface.labelTGeneralLanguage.Content = ReadLangWithParameter("ToGeLT");

            OledInterface.labelTMessagesTitle.Content = ReadLangWithParameter("ToMeTi");
            OledInterface.labelTMessagesWelcomeAndBye.Content = ReadLangWithParameter("ToMeST");
            OledInterface.radioButtonToolsMessages.Content = ReadLangWithParameter("ToMeSm");
            OledInterface.radioButtonToolsLogo.Content = ReadLangWithParameter("ToMeSl");
            OledInterface.labelTMessagesText.Content = ReadLangWithParameter("ToMeTT");
            OledInterface.radioButtonToolsMessagesTextBig.Content = ReadLangWithParameter("ToMeTb");
            OledInterface.radioButtonToolsMessagesTextMedium.Content = ReadLangWithParameter("ToMeTm");
            OledInterface.radioButtonToolsMessagesTextSmall.Content = ReadLangWithParameter("ToMeTs");

            OledInterface.labelTThemesTitle.Content = ReadLangWithParameter("ToThTi");

            OledInterface.labelTWeatherTitle.Content = ReadLangWithParameter("ToWeTi");
            OledInterface.labelTWeatherCity.Content = ReadLangWithParameter("ToWeCi");
            OledInterface.labelTWeatherTemperature.Content = ReadLangWithParameter("ToWeTe");
            OledInterface.radioTemperatureCelcius.Content = ReadLangWithParameter("ToWeTC");
            OledInterface.radioTemperatureFarenheit.Content = ReadLangWithParameter("ToWeTF");

            //-- buttons
            OledInterface.buttonCGeneral.Content = ReadLangWithParameter("ToGeBu");
            OledInterface.buttonCMessages.Content = ReadLangWithParameter("ToMeBu");
            OledInterface.buttonCThemes.Content = ReadLangWithParameter("ToThBu");
            OledInterface.buttonCWeather.Content = ReadLangWithParameter("ToWeBu");
            //--
            #endregion "tools"

            //--
            #endregion "pages"

            #region "navigation"
            //--

            #region "normal"
            //--
            OledInterface.buttonNavigationStatus.Content = ReadLangWithParameter("NaNoSt");
            OledInterface.buttonNavigationUpdate.Content = ReadLangWithParameter("NaNoUp");
            OledInterface.buttonNavigationAbout.Content = ReadLangWithParameter("NaNoAb");
            OledInterface.buttonNavigationOledTools.Content = ReadLangWithParameter("NaNoTo");
            //--
            #endregion "normal"

            #region "tools"
            //--
            OledInterface.buttonToolsRestore.Content = ReadLangWithParameter("NaToRe");
            //--
            #endregion "tools"

            //--
            #endregion "navigation"
        }
        public string ReadLangWithParameter(string Parameter)
        {
            int i = 0;
            string firstPara;
            string line;
            string parameterData = "";
            bool found = false;

            using (StreamReader fileReader = new StreamReader(PathToLangFile, Encoding.Default))
            {
                while (((line = fileReader.ReadLine()) != null) && i < 255 && !found)
                {
                    i++;

                    firstPara = line[0].ToString();
                    for (int charI = 1; charI < 6; charI++)
                        firstPara += line[charI].ToString();

                    if (firstPara.Equals(Parameter))
                    {
                        found = true;

                        // Read the data
                        if (line[9] == '|')
                        {
                            int iM = 10;

                            while (line[iM] != '|')
                            {
                                parameterData += line[iM];
                                iM++;
                            }
                        }
                    }
                }
            }
            if (!found)
                Log.Write("Error trying to access parameter: " + Parameter);
            //-- return
            return (parameterData);
        }
        //--
        #endregion "Methods"
    }

    /// <summary>
    ///  Class to read the number of languages and display them on the API
    /// Link to the files .\Lang\languages.txt
    /// </summary>
    public class ReadLanguages
    {
        #region "Attributes"
        //--
        public string Languages = "";
        //--
        #endregion "Attributes"

        #region "Constructor"
        //--
        public ReadLanguages (OmauraOled OledInterface)
        {
            // parameters definition
            int i = 0;
            string line;
           
            using (StreamReader fileLangReader = new StreamReader(".\\Resources\\Lang\\Languages.txt"))
            { 
                while (((line = fileLangReader.ReadLine()) != null) && i < 255)
                {
                    // Overflow counter
                    i++;

                    // Interprete the line
                    if (line[0].Equals('|'))
                    {
                        int iChar = 8;
                        string languageTemp = "";

                        // get full text of the languages name
                        while (!line[iChar].Equals('|'))
                        {
                            languageTemp += line[iChar].ToString();
                            iChar++;
                        }
                        // Add to the API combobox
                        OledInterface.comboBoxLanguage.Items.Add(languageTemp);
                    }
                }
            }
        }
        //--
        #endregion "Constructor"

        #region "Methods"
        //--
        public string Get2LettersForLanguage(string LanguagePara)
        {            
            int i = 0;
            bool findLanguage = false;
            string line;
            string LanguageReturn = "";
           
            using(StreamReader fileLangReader = new StreamReader(".\\Resources\\Lang\\Languages.txt", Encoding.Default))
            {
                while (((line = fileLangReader.ReadLine()) != null) && i < 255 && !findLanguage)
                {

                    i++;

                    if (line[7] == '|')
                    {
                        int CharI = 8;
                        string LanguageTemp = "";

                        while (line[CharI] != '|')
                        {
                            LanguageTemp += line[CharI];
                            CharI++;
                        }

                        if (LanguageTemp.Equals(LanguagePara))
                        {
                            LanguageReturn = line[1].ToString() + line[2].ToString();
                            findLanguage = true;
                        }
                    }
                }
            }
            // return value
            if (findLanguage)
                return LanguageReturn;
            else
                return "error";

        }
        //--
        #endregion "Methods"
    }
}